import {queryPerson, addPerson, getPerson, putPerson, delPerson} from '../services/labor';

export default {
  namespace: "laborPerson",
  state: {
    data: {
      list: [],
      pagination: {},
    },
    formValues: {},
  },
  effects: {
    * fetch({payload}, {call, put}) {
      let params = {}
      if (!payload.orderByField) {
        params = {
          ...payload,
          orderByField: 'createTime',
          isAsc: false,
        }
      } else {
        params = payload
      }
      const response = yield call(queryPerson, params);
      if (response) {
        yield put({
          type: 'saveList',
          payload: response,
        });
      }
    },
    * add({payload, callback}, {select, call, put}) {
      const response = yield call(addPerson, payload);
      if (response && response.code === 0) {
        const pagination = yield select(state => state.laborPerson.data.pagination);
        const params = {
          page: pagination.current,
          limit: pagination.pageSize,
        };
        const data = yield call(queryPerson, params);
        if (data) {
          yield put({
            type: 'saveList',
            payload: data,
          });
          if (callback) callback()
        }
      }
    },
    * new({payload, callback}, {put}) {
      yield put({
        type: 'saveFormValue',
        payload: payload,
      });
      if (callback) callback()
    },
    * get({payload, callback}, {call, put}) {
      const response = yield call(getPerson, payload);
      if (response && response.code === 0) {
        yield put({
          type: 'saveFormValue',
          payload: response.data,
        });
        if (callback) callback()
      }
    },
    * change({payload}, {put}) {
      yield put({
        type: 'changeFormValue',
        payload: payload,
      });
    },
    * edit({payload, callback}, {call, put}) {
      const response = yield call(putPerson, payload)

      if (response && response.code === 0) {

        yield put({
          type: 'saveFormValue',
          payload: payload,
        });
        yield put({
          type: 'editChange',
          payload: payload,
        });
        if (callback) callback()
      }
    },
    * remove({payload, callback}, {call, put}) {
      const response = yield call(delPerson, payload.id)

      if (response && response.code === 0) {
        yield put({
          type: 'delChange',
          payload: payload,
        });
        if (callback) callback()
      }
    },
  },
  reducers: {
    saveList(state, action) {
      return {
        ...state,
        data: {
          list: action.payload.records,
          pagination: {
            total: action.payload.total,
            pageSize: action.payload.size,
            current: action.payload.current,
          },
        },
      };
    },
    changeFormValue(state, action) {
      return {
        ...state,
        formValues: {...state.formValues, ...action.payload},
      };
    },
    saveFormValue(state, action) {
      return {
        ...state,
        formValues: action.payload,
      };
    },
    addChange(state, action) {
      state.data.list.push(action.payload)
      return {
        ...state,
      };
    },
    editChange(state, action) {
      state.data.list.map((item, key) => {
        if (item.id == action.payload.id) {
          state.data.list[key] = {...state.data.list[key], ...action.payload}
          return
        }
      })

      return {
        ...state,
      };
    },
    delChange(state, action) {
      state.data.list = state.data.list.filter((item) => {
        if (item.id != action.payload.id) {
          return item
        }
      })
      return {
        ...state,
      };
    },
  },
};
